%LDA (Linear Discriminant Algorithm)
%trainData1 and trainData2 are the datasets with characteristics in row and trials in column;
%w is the projection vector, usage: w'*your_data;
%projMu1 and projMu2 are the mean value of the projected data;
%projVar1 and projVar2 are the variance of the projected data;

function [w,projMu1,projMu2,projVar1,projVar2] = linear_discriminant_analysis(trainData1,trainData2)
%     trainData1 = Data(:,find(label(train)==1)); Please remember this
%     shit bug forever
outlier1 = isoutlier(trainData1,2);
outlier2 = isoutlier(trainData2,2);
trainData1 = trainData1(:,find(sum(outlier1) == 0));
trainData2 = trainData2(:,find(sum(outlier2) == 0));
mu1 = mean(trainData1,2);
mu2 = mean(trainData2,2);
cov1 = (trainData1-mu1)*(trainData1-mu1)';
cov2 = (trainData2-mu2)*(trainData2-mu2)';
Sw = cov1 + cov2;
w = pinv(Sw)*(mu1-mu2); %can be replaced by SVD algorithm for numeric stability;
convTrainData1 = w'*trainData1;
convTrainData2 = w'*trainData2;
projMu1 = mean(convTrainData1);
projMu2 = mean(convTrainData2);
projVar1 = mean((convTrainData1-projMu1).^2);
projVar2 = mean((convTrainData2-projMu2).^2);
end